<template>
  <div class="dashboard-container">
    <el-table :data="list">
        <el-table-column label="模板名称" prop="objectName"></el-table-column>
        <el-table-column label="操作">
            <template slot-scope="{row}">
                <el-button type="text" v-if="row.filePath"><a :href="`javascript:POBrowser.openWindow('${API_BASE_URL}/pageOffice/openLocalFile?filePath=${row.filePath}&editFlag=1&fileTitle='+ encodeURIComponent('${row.objectName}'), 'width=1200px;height=900px;');`">打开</a></el-button>
                <el-button type="text" v-if="row.filePath" @click="refresh(row)">同步</el-button>
            </template>
        </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { fieldGetFieldTemplatePath, fieldAsyncRefreshTemplateFields } from '@/api/field'
// import { POBrowser } from 'js-pageoffice'
import { OBJECT_ALL_TYPE_LIST, API_BASE_URL } from '@/utils/common'
import { transfromData } from '@/utils/tools'

export default {
  name: 'Template',
  data() {
    return {
        API_BASE_URL,
        api_url: '',
        OBJECT_ALL_TYPE_LIST,
        list: []
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 同步字段
    async refresh(row){
        let params = {
            objectType: row.objectType,
        }
        if(row.propertyType){
            params.propertyType = row.propertyType
        }
        await fieldAsyncRefreshTemplateFields(params)
        this.$message.success({message: '刷新成功'})
    },
    async getData(){
        let data = await fieldGetFieldTemplatePath()
        this.list = data.map(item=>{
          let str = '';
          if(item.propertyTypeString){
            str = '-' + item.propertyTypeString
          }
          item.objectName = transfromData(OBJECT_ALL_TYPE_LIST, item.objectType) + str;
          return item;
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
    position: relative;
    .page {
      padding: 20px 0;
      text-align: center;
    }
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>